package com.cms.server.resolver.plugins.scsq;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import com.efounder.builder.base.data.DataSetUtils;
import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.eai.data.JParamObject;
import com.efounder.form.EFFormDataModel;
import com.efounder.form.io.FormDataResolverAdapter;
import com.efounder.form.server.resolver.util.SYS_MDL_VAL;
import com.efounder.sql.JStatement;
import com.metadata.bizmodel.SYS_MODEL;
import com.persistence.FLOW_TASK_LIST;

/**
 * 单据保存，进入流程节点
 * @author Administrator
 *
 */
public class CMSSaveSCSQFormToFlow extends FormDataResolverAdapter {

	//初始化FormDataModel
	public void finishSaveForm(JStatement stmt, EFFormDataModel model, JParamObject PO) throws Exception {
		EFDataSet                    billDataSet = model.getBillDataSet();
		EFRowSet                      nodeRowSet = EFRowSet.getInstance();
		String                            strSql = "";
		ResultSet                             rs = null;
		long                             curTime = new Date().getTime();
		String                            F_KJQJ = billDataSet.getRowSet(0).getString(PO.GetValueByParamName(SYS_MODEL._BLFL_KJQJ_COL_, SYS_MDL_VAL.BILL_KJQJ), "");
		String                            F_GUID = billDataSet.getRowSet(0).getString(PO.GetValueByParamName(SYS_MODEL._BILL_GUID_COL_, SYS_MDL_VAL.BILL_GUID), "");
		String                            F_DJBH = billDataSet.getRowSet(0).getString(PO.GetValueByParamName(SYS_MODEL._BILL_BH_COL_, SYS_MDL_VAL.BILL_BH), null);
		String                           FLOW_ID = PO.GetValueByParamName(FLOW_TASK_LIST._FLOW_ID, "");

		//获取提交节点名称
		strSql = "select * from FLOW_NODE_LIST where FLOW_ID ='" + FLOW_ID + "' and NODE_ID = '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_NODE, "") + "'";
		rs = stmt.executeQuery(strSql);
		if(rs != null && rs.next()) nodeRowSet = (EFRowSet) DataSetUtils.resultSet2RowSet(rs, nodeRowSet);
		
		//如果流程结束表里已经有记录，则说明在此次操作之前，已经走完流程，则直接返回
		strSql = " select * from FLOW_TASK_END_LIST where BIZ_DATE = '" + F_KJQJ + "' and BIZ_DJBH = '" + F_DJBH + "' and "
			   + " FLOW_ID = '" + FLOW_ID + "'";
		rs = stmt.executeQuery(strSql);
		if(rs != null && rs.next()) return;
		
		strSql = " select * from FLOW_TASK_LIST where BIZ_DATE = '" + F_KJQJ + "' and BIZ_DJBH = '" + F_DJBH + "' and "
			   + " FLOW_ID = '" + FLOW_ID + "' and NODE_TAG = '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_NODE, "") + "' order by (OP_ID + 0) desc";
		rs = stmt.executeQuery(strSql);
		

		//开始节点单独处理，保存后就要进入流程任务列表，只要保存一次，以后就不再更新
		if(rs == null || !rs.next()) {
			strSql = "insert into FLOW_TASK_LIST (OP_ID,FLOW_ID,BIZ_DJBH,BIZ_DATE,BIZ_GUID,OP_USER,OP_USER_NAME,OP_TIME,NODE_TAG,NODE_TAG_NAME,"
				   + "OP_BMBH,OP_NOTE,TASK_STATUS,OP_NODE,NODE_SRC,NODE_SRC_NAME,POP_ID,RESR_STATUS,";
			for(int j = 1; j <= 20; j++) {
				if(j < 10) {
					if(PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + "0" + j, null) != null) {
						strSql += "F_CHAR0" + j + ",";
					}
				} else {
					if(PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + j, null) != null) {
						strSql += "F_CHAR" + j + ",";
					}
				}
				if(j < 10) {
					if(PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + "0" + j, null) != null) {
						strSql += "F_DATE0" + j + ",";
					}
				} else {
					if(PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + j, null) != null) {
						strSql += "F_DATE" + j + ",";
					}
			    }
				if(j < 10) {
					if(PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + "0" + j, null) != null) {
						strSql += "F_NUM0" + j + ",";
					}
				} else {
					if(PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + j, null) != null) {
						strSql += "F_NUM" + j + ",";
					}
				}
			}
			
			strSql += "RESR_IN_CAUSE,RESR_OUT_CAUSE) values ('" 
				    + curTime + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._FLOW_ID, "") + "', '"
				    + F_DJBH + "', '" + F_KJQJ + "', '" + F_GUID + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_USER, "")
				    + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_USER_NAME, "") + "', '"
				    + curTime + "','" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_NODE, "") + "','" + nodeRowSet.getString("NODE_NAME", "") 
  	 			    + "','" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_BMBH, "") + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_NOTE, "") + "', 'processing', '"
	 			    + PO.GetValueByParamName(FLOW_TASK_LIST._OP_NODE, "") + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_NODE, "") + "', '"
				    + nodeRowSet.getString("NODE_NAME", "")  + "', '', '" + PO.GetValueByParamName(FLOW_TASK_LIST._RESR_STATUS, "") 
				    + "', ";
			for(int j = 1; j <= 20; j++) {
				if(j < 10) {
					if(PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + "0" + j, null) != null) {
						strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + "0" + j, null)  + "',";
					}
				} else {
					if(PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + j, null) != null) {
						strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + j, null) + "',";
					}
				}
				
				if(j < 10) {
					if(PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + "0" + j, null) != null) {
						strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + "0" + j, null)  + "',";
					}
				} else {
					if(PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + j, null) != null) {
						strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + j, null)  + "',";
					}
				}
				if(j < 10) {
					if(PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + "0" + j, null) != null) {
						strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + "0" + j, null)  + "',";
					}
				} else {
					if(PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + j, null) != null) {
						strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + j, null)  + "',";
					}
				}			    	
			}
			strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._RESR_IN_CAUSE, "") + "', '')";
			stmt.execute(strSql);
		}
	}
}
